package com.chuangke.common.db.page;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

public class CkPageQuery extends HashMap<String, Object> {

	
    private static final long serialVersionUID = 1084807919843629085L;

	private static final Integer PAGE_CURRENT = 1;// 当前页码
	private static final Integer PAGE_SIZE = 10; // 每页条数

	public CkPageQuery(Map<String, Object> params) {
		this.putAll(params);
	}

	public <T> IPage<T> getPage() {
		return new Page<>(getPageCurrent(), getPageSize());
	}

	public int getPageCurrent() {
		Integer current = (Integer) this.get("current");
		return current == null ? PAGE_CURRENT : current;
	}

	public int getPageSize() {
		Integer size = (Integer) this.get("size");
		return size == null ? PAGE_SIZE : size;
	}

	public Map<String, Object> getQueryParams() {
		List<String> pageParam = Arrays.asList("current", "size");
		Map<String, Object> queryParams = new HashMap<>();
		for (Map.Entry<String, Object> entry : this.entrySet()) {
			if (!pageParam.contains(entry.getKey())) {
				queryParams.put(entry.getKey(), entry.getValue());
			}
		}
		return queryParams;
	}
	
	public String getAsString(String key) {
		if (this.get(key) == null) {
			return null;
		}
		return (String)this.get(key);
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getAsListString(String key) {
		if (this.get(key) == null) {
			return null;
		}
		return (List<String>)this.get(key);
	}

	@SuppressWarnings("rawtypes")
	public static <T> IPage<T> cloneNew(IPage page) {
		IPage<T> newPage = new Page<>();
		newPage.setCurrent(page.getCurrent());
		newPage.setPages(page.getPages());
		newPage.setSize(page.getSize());
		newPage.setTotal(page.getTotal());
		return newPage;
	}

	public static <T> CkPageQuery clone(CkPageQuery query) {
		return new CkPageQuery(query);
	}

}
